/**
 * 上传文件插件 细分为传图和传文件
 * 没有flag概念
 * 比如说要传车前/车身/车后三种图, 初始化三次插件
 * 预览模式时, 如果结尾是图片, 预览, 如果是文件, 显示文件类型
 * TODO 文件名
 */
var plugin_upload_file = {
	config:{
		selector:"#upload_image_container",
		attachment_key:"",
		attachment_pkid:"",
		attachment_business:"",
		single_mode:true,
		files:[],
		upload_param:{
			url:"/sys/attachment/upload_image",
			field:"file",
		},
	},
	_cache:{},
	
	// 初始化
	init:function(param){
		var _this = this;
		_this.destory();// 先销毁
		
		$.extend(_this.config,param);
		
		// 加载文件上传配置
		_this.load_upload_setting(function(_config){
			// 生成页面元素
			_this._create_element(_config);
			
			// 为页面元素绑定事件
			_this._bind_event();
		});
		
		
	},
	
	// 销毁
	destory:function(){
		var _this = this;
		// 取消事件
		$(_this.config.selector+"_input").unbind("change");
		$(_this.config.selector).find(".upload_btn").unbind("click");
		
		// 清空选择器的内容
		$(_this.config.selector).html("");
		
		// 删掉input file
		$(_this.config.selector+"_input").remove();
	},
	
	// 加载文件上传配置
	load_upload_setting:function(_callback){
		var _config = undefined;
		_callback(_config);
	},
	
	// 上传结果, 写入数据库
	write_db:function(){},
	
	// 详情页调用
	detail_show_file:function(){},
	
	// 上传完成后的回显
	_echo_display:function(){
		// TODO 判断文件类型, 图片显示缩略图, 文件显示列表
	},
	
	// 生成页面元素
	_create_element:function(_config){
		var _this = this;
		
		// TODO 判断需要上传的文件类型, 图片或者文件, 添加不同的class, 用class控制回显时的不同样式
		var _class = "type_file or type_image";
		
		$(_this.config.selector)
			.addClass(_class)
			.append($("<div class='upload_echo_container'></div>"))
			.append($("<div class='upload_btns_container'></div>")
				.append($("<div class='upload_btn'><i class='fa fa-upload'></i><span>选择文件</span></div>")));
		$("body").append($("<input type='file' id='"+_this.config.selector+"_input' />").css("display","none"));
		
		// 绑定事件
		_this._bind_event();
	},
	
	// 上传事件
	_bind_event:function(){
		var _this = this;
		$(_this.config.selector).find(".upload_btns_container .upload_btn")
			.unbind("click")
			.bind("click",function(){
				_this._cache.type = $(this).find("span").html();
				document.getElementById('input_upload_file').click();
			});
		
		$(_this.config.selector+"_input").unbind("change").bind("change",function(){
			_this._upload();
		});
	},
	
	// 上传
	_upload:function(){
		var _this = this;
		var _form_data = new FormData();
		
		_form_data.append("file", document.getElementById("input_upload_file").files[0]);
		
		if(!_this._validate_img(_form_data)){
			return;
		}
		
		common_util.exec_upload(_this.config.upload_param.url,_form_data,function(_d){
			common_util.tip(_d.text);
			if(_d.code!=200)return;
			
			_d.data.temp = true;
			_d.data.id = Math.random();
			_d.data.type = _this._cache.type;
			
			_this.config.images.push(_d.data);
			_this._echo_display();
		});
	},
	
	// 验证上传格式大小是否符合要求
	_validate_img:function(_d){
		var _file = _d.get("file");
		
		if(!/.(jpg|bmp|png)$/.test((_file.name+"").toLowerCase())){
			common_util.tip("图片请选择格式为jpg , bmp , png")
			return false;
		}
		return true;
	},
	
	
	
	
	
	
	
	
	init:function(param){
		var _this = this;
		_this.destroy();
		
		$.extend(_this.config,param);
		
		_this._init_element();
		_this._init_btn_event();
		_this._init_file_event();
		
		if(_this.config.attachment_key.length>0&&_this.config.attachment_pkid.length>0){
			var _data = {"ascription":_this.config.attachment_key};
			_data.pkid = _this.config.attachment_pkid;
			common_util.exec("/sys/attachment/query",_data,function(_d){
				console.log("d",_d);
				for(i in _d.rows){
					var _item = {};
					_item.id = _d.rows[i].id;
					_item.big_url = _d.rows[i].filePath;
					_item.small_url = _d.rows[i].previewPath;
					_item.type = _d.rows[i].classes;
					_item.temp = false;
					_this.config.images.push(_item);
				}
				
				_this._echo_display();
			});
		}
		
		_this._echo_display();
	},
	get_images:function(){
		return this.config.images;
	},
	destroy:function(){
		var _this = this;
		_this.config.images.length=0;
		_this.config.attachment_key = "";
		_this.config.attachment_pkid = "";
		_this.config.attachment_business = "";
		$("#input_upload_file").remove();
		$(_this.config.selector).html("");
		$(".viewer-container.viewer-hide").remove();
	},
	// 批量插入附件表
	insert_attachment:function(_ascription,_pkid,_business){
		var _this = this;
		var temp = new Array();
		for(i in _this.config.images){
			if(!_this.config.images[i].temp)continue;
			
			temp.push(_this.config.images[i]);
		}
		
		if(temp.length<=0)return;
		
		var _data = {"ascription":_ascription,"pkid":_pkid,"business":_business};
		_data.images = temp;
		
		common_util.exec("/sys/attachment/batch_create",_data,function(_d){
			// TODO 
		});
	},
	detail_show_images:{
		images:[],
		conf:{},
		init:function(_selector,_key,_pkid){
			var _this = this;
			_this._destory();
			
			_this.conf.selector = _selector;
			_this.conf.key = _key;
			_this.conf.pkid = _pkid;
			
			common_util.exec("/sys/attachment/query",{"ascription":_key,"pkid":_pkid},function(_d){
				for(i in _d.rows){
					var _item = {};
					_item.id = _d.rows[i].id;
					_item.big_url = _d.rows[i].filePath;
					_item.small_url = _d.rows[i].previewPath;
					_item.type = _d.rows[i].classes;
					_this.images.push(_item);
				}
				
				_this._echo_display();
			});
		},
		_echo_display:function(){
			var _this = this;
			
			for(i in _this.images){
				$(_this.conf.selector).append($("<div class='item_img'></div>")
						.attr("data-id",_this.images[i].id)
						.append($("<img />").attr("data-original",img_prefix+_this.images[i].big_url)
							.attr("src",img_prefix+_this.images[i].small_url)
							.attr("alt",_this.images[i].type)
							.attr("title",_this.images[i].type))
						.append($("<span></span>").html(_this.images[i].type)));
			}
			
			$(_this.conf.selector).viewer({
				url:"data-original"
			});
			
			common_util.image_load_error();
		},
		_destory:function(){
			var _this = this;
			
			_this.images.length=0;
			$(_this.conf.selector).html("");
		},
	},
	_init_element:function(){
		var _this = this;
		
		var _continer = $(_this.config.selector);
		_continer.append($("<div class='upload_echo_container'></div>"));
		_continer.append($("<div class='upload_btns_container'></div>"));
		var _btns = _continer.find(".upload_btns_container");
		for(i in _this.config.btns){
			$("<div class='upload_btn'></div>")
				.append($("<i class='"+_this.config.btns[i].icon+"'></i>"))
				.append($("<span>"+_this.config.btns[i].text+"</span>"))
				.appendTo(_btns);
		}
		$("body").append($("<input type='file' id='input_upload_file' />").css("display","none"));
	},
	_init_btn_event:function(){
		var _this = this;
		$(_this.config.selector).find(".upload_btns_container .upload_btn")
			.unbind("click")
			.bind("click",function(){
				_this._cache.type = $(this).find("span").html();
				document.getElementById('input_upload_file').click();
			});
	},
	_init_file_event:function(){
		var _this = this;
		$("#input_upload_file").unbind("change").bind("change",function(){
			_this._upload();
		});
	},
	_upload:function(){
		
	},
	_validate_img:function(_d){
		
	},
	_echo_display:function(){
		var _this = this;
		
		$(_this.config.selector).find(".upload_echo_container").html("");
		
		for(i in _this.config.images){
			_this._echo_item(_this.config.images[i]);
		}
		_this._echo_item_event();
	},
	_echo_item:function(_d){
		var _this = this;
		console.log("_echo_item",_d);
		$(_this.config.selector).find(".upload_echo_container")
			.append($("<div class='item_img'></div>")
				.attr("data-id",_d.id)
				.append($("<img />").attr("src",img_prefix+_d.small_url))
				.append($("<i class='fa fa-close'></i>"))
				.append($("<span></span>").html(_d.type)));
	},
	_echo_item_event:function(){
		var _this = this;
		$(_this.config.selector).find(".item_img i").unbind("click").bind("click",function(){
			if(!confirm("是否确认删除? "))return;
			
			var _id = $(this).parent().attr("data-id");
			var _item = {};
			for(i in _this.config.images){
				if(_this.config.images[i].id!=_id)continue;
				
				_item = _this.config.images[i];
				_this.config.images.splice(i,1);
			}
			
			_this._echo_display();
			
			if(_item.temp)return;
			
			common_util.exec("/sys/attachment/delete",{"id":_id},function(_d){
				console.log("del",_d);
			});
		});
	},
};
